package com.xn.app.muying.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.xn.app.muying.R;
import com.xn.app.muying.adpter.DoctorLeftAdapter;
import com.xn.app.muying.bean.DoctorGsonBean;
import com.xn.app.muying.fragment.DoctorFragment;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/31.
 */
public class DoctorActivity extends AppCompatActivity implements HttpUtil.OnGetDataListener {

    private ListView lv;
    private Handler handler = new Handler();
    private DoctorLeftAdapter doctorLeftAdapter;
    private LinearLayout ll;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor);
        lv = (ListView) findViewById(R.id.activity_doctor_listview);
        ll = (LinearLayout) findViewById(R.id.activity_doctor_ll);
        HttpUtil.getData(APIMethod.Home_Doctor_Left,handler,this);
        doctorLeftAdapter = new DoctorLeftAdapter(DoctorActivity.this);
        lv.setAdapter(doctorLeftAdapter);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        DoctorFragment doctorFragment = new DoctorFragment();
        transaction.replace(R.id.activity_doctor_ll,doctorFragment).commit();
    }

    @Override
    public void onJsonData(String data) {
        ArrayList<DoctorGsonBean> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(data);
            JSONArray items = object.getJSONArray("items");
            for (int i = 0; i < items.length(); i++) {
                list.add(new Gson().fromJson(items.get(i).toString(),DoctorGsonBean.class));
            }

            doctorLeftAdapter.getData(list);
            Log.e("data22",list.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
